package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工接口")//day01Swagger的使用
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;


    /**
     * 更新员工(day02任务四)
     *
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新员工")
    public Result updateEmpById(@RequestBody EmployeeDTO employeeDTO) {
        log.info("[更新员工] emp:{}", employeeDTO);

        employeeService.updateEmp(employeeDTO);
        return Result.success();
    }

    /**
     * 根据ID获取员工信息(day02任务四)
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取员工")
    public Result<Employee> findById(@PathVariable Long id) {
        log.info("[基于ID获取] id:{}", id);

        Employee employee = employeeService.findById(id);

        return Result.success(employee);
    }

    /**
     * 启动禁用员工账号(day02任务三)
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改员工状态")
    public Result setEmpStatus(@PathVariable Integer status, Integer id) {
        log.info("[修改员工状态],status:{},id:{}", status, id);
        employeeService.setEmpStatus(status, id);
        return Result.success();
    }

    /**
     * 员工分页查询(day02任务二)
     *
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> findByPage(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("[员工分页查询] pageDTO:{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.findByPage(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增员工(day02任务一)
     *
     * @param employeeDTO
     * @return
     */
    @PostMapping
   // @ApiOperation("新增员工")
    @ApiOperation(value = "新增员工")
    public Result addEmp(@RequestBody EmployeeDTO employeeDTO) {
        log.info("[新增员工] empDTO:{}", employeeDTO);
        Long empId = BaseContext.threadLocalEmpId.get();
        String username = BaseContext.threadLocalEmpUserName.get();
        log.info("[当前线程],thread:{} empId:{},empUsername:{}", Thread.currentThread().getId(), empId, username);

        employeeService.addEmp(employeeDTO);
        return Result.success();
    }

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录接口")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        claims.put(JwtClaimsConstant.USERNAME, employee.getUsername());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        EmployeeLoginVO employeeLogin = new EmployeeLoginVO();
        employeeLogin.setId(employee.getId());
        employeeLogin.setUserName(employee.getUsername());
        employeeLogin.setName(employee.getName());
        employeeLogin.setToken(token);

        return Result.success(employeeLogin);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录接口")
    public Result<String> logout() {
        return Result.success();
    }

}
